package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

public class TestThreadPool {

    public static void main(String[] args) throws ExecutionException, InterruptedException {



        ExecutorService executor = Executors.newFixedThreadPool(10);

        System.out.println("main....start.....");

//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//
//            int i = 10 / 2;
//
//            System.out.println("运行结果：" + i);
//
//        }, executor);

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//
//            System.out.println("当前线程：" + Thread.currentThread().getId());
////
//            int i = 10 / 0;
//
//            System.out.println("运行结果：" + i);
//
//            return i;
//        }, executor).whenComplete((res, exception) -> {
//            System.out.println("异步任务完成了..." + "结果是：" + res + "异常是：" + exception);
//        }).exceptionally(throwable -> {
//            return 10;
//        });
//
//        Integer i = future.get();
//
//        System.out.println("i的值是："+i);

        /**
         * 方法执行完成后的处理
         */
//
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(()->{
//            System.out.println("当前线程："+Thread.currentThread().getId());
//
//            int i = 10/0;
//
//            System.out.println("运行结果：" + i);
//
//            return  i;
//
//        },executor).handle((res,thr)->{
//
//            if(res != null ){
//
//                return  res*2;
//            }
//            if(thr != null){
//                return  0;
//
//            }
//
//            return  0;
//        });
//
//
//        Integer integer = future.get();
//        System.out.println("main的result：" + integer);
//
//
//
//    }

        CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {

            System.out.println("线程一运行开始");

            Integer i = 10 / 2;

            System.out.println("运算值为：" + i);

            return i;
        }, executor);


        CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {

            System.out.println("任务线程2：" + Thread.currentThread().getName());


            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务线程2结束");

            return  "hello";
        }, executor);

//
//        future1.thenAcceptBothAsync(future2,(f1,f2)->{
//            System.out.println("任务3开始" + f1 + "......>" + f2);
//        });
//
//
//        CompletableFuture<String> stringCompletableFuture = future1.thenCombineAsync(future2, (f1, f2) -> {
//            return f1 + ":" + f2 + "-> haha";
//        }, executor);

//        future1.runAfterEitherAsync(future2,()->{
//            System.out.println("任务3开始执行..");
//
//        },executor);

//        future1.acceptEitherAsync(future2,(res)->{
//
//            System.out.println("任务3执行" + res);
//
//        });

//        CompletableFuture<String> stringCompletableFuture = future1.applyToEitherAsync(future2, (res) -> {
//            System.out.println("任务3运行中" + res.toString());
//
//            return res.toString();
//        }, executor);
//
//
//        System.out.println("main end。。。" + stringCompletableFuture.get() );

        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品信息");

            return "hello";
        });

        CompletableFuture<String> f2= CompletableFuture.supplyAsync(() -> {
            System.out.println("查询品类信息");

            return "hello1";
        });

        CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询品牌信息");

            return "hello2";
        });

//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(f1, f2, f3);

        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(f1, f2, f3);

        Object o = objectCompletableFuture.get();

        System.out.println("main任务运行中》》》" + o);

        System.out.println(f1.get() +">>>" + f2.get() +">>>>" + f3.get());


    }



}
